package b2.widget.fight
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.fight.*;
	import b2.util.*;
	import b2.widget.*;
	
	import flash.display.Sprite;
	import flash.filters.*;
	import flash.text.*;

	import org.flixel.*;
	
	public class BattleRightPane extends Sprite
	{
		public var healthPointBar:ProgressBar;
		public var energyPointBar:ProgressBar;
		public var hpBottomText:TextField;
		public var epBottomText:TextField;
		private var weaponSlots:Object;
		
		public function BattleRightPane()
		{
			super();
			weaponSlots = new Object();
			//背景图片
			var BattlePaneBgr:Class = ResourceHelper.getInstance().getClassByName("BattleRightPane");
			var battlePaneBgr:Sprite = new BattlePaneBgr();
			addChild(battlePaneBgr);
			//生命条
			var VHpBarFrame:Class = ResourceHelper.getInstance().getClassByName("VHpBarFrame");
			var VHpBar:Class = ResourceHelper.getInstance().getClassByName("VHpBar");
			healthPointBar = new ProgressBar(VHpBarFrame, VHpBar, 1, 5, true);
			healthPointBar.x = 17;
			healthPointBar.y = 40;
			addChild(healthPointBar);
			//能量条
			var VEpBar:Class = ResourceHelper.getInstance().getClassByName("VEpBar");
			energyPointBar = new ProgressBar(VHpBarFrame, VEpBar, 1, 5, true);
			energyPointBar.x = 57;
			energyPointBar.y = 40;
			addChild(energyPointBar);
			//生命
			var hpTopText:TextField = new TextField();
			hpTopText.selectable = false;
        	hpTopText.autoSize = TextFieldAutoSize.LEFT;
			hpTopText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFF0000);
			hpTopText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			hpTopText.text = "生命";
			hpTopText.x = 20;
			hpTopText.y = 15;
			addChild(hpTopText);
			hpBottomText = new TextField();
			hpBottomText.selectable = false;
        	hpBottomText.autoSize = TextFieldAutoSize.CENTER;
			hpBottomText.defaultTextFormat = new TextFormat("微软雅黑", 10, 0xFF0000);
			hpBottomText.x = 28;
			hpBottomText.y = 220;
			addChild(hpBottomText);
			//能量
			var epTopText:TextField = new TextField();
			epTopText.selectable = false;
        	epTopText.autoSize = TextFieldAutoSize.LEFT;
			epTopText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x0000FF);
			epTopText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			epTopText.text = "能量";
			epTopText.x = 57;
			epTopText.y = 15;
			addChild(epTopText);
			epBottomText = new TextField();
			epBottomText.selectable = false;
        	epBottomText.autoSize = TextFieldAutoSize.LEFT;
			epBottomText.defaultTextFormat = new TextFormat("微软雅黑", 10, 0x0000FF);
			epBottomText.x = 57;
			epBottomText.y = 220;
			addChild(epBottomText);
		}
		
		public function onTankCreated(event:FightEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (me.campTankId == event.targetTankId)
			{
				refresh();
			}
		}
		
		public function onClockTick(event:FightEvent):void
		{
			for (var positionStr:String in weaponSlots)
			{
				var weaponSlot:WeaponSlot = weaponSlots[positionStr];
				if (weaponSlot.cooldown > 0)
					weaponSlot.cooldown--;
			}
		}
		
		public function onTankStatusChange(event:FightEvent):void
		{
			var state:PlayState = FlxG.state as PlayState;
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var myCampTank:TankSprite = state.tanks[me.campTankId];
			if (myCampTank.tankId == event.targetTankId)
			{ 
				if (event.hpDelta != 0)
				{
					healthPointBar.percentage = event.healthPoint / myCampTank.healthPoint;
					hpBottomText.text = event.healthPoint.toString();
				}
				if (event.epDelta != 0)
				{
					energyPointBar.percentage = event.energyPoint / myCampTank.energyPoint;
					epBottomText.text = event.energyPoint.toString();
					//更新武器图标
					for (var positionStr:String in weaponSlots)
					{
						var weaponSlot:WeaponSlot = weaponSlots[positionStr];
						var equipConfig:EquipConfig = Config.objectConfigs[weaponSlot.objectIndex];
						if (equipConfig.energyCost[weaponSlot.basicLevel * 5 + weaponSlot.strengthenLevel] > myCampTank.currentEp)
							weaponSlot.disable = true;
						else
							weaponSlot.disable = false; 
					}
				}
			}
		}
		
		public function refresh():void
		{
			//刷新生命和能量
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var myCampTank:TankInfo = PlayerInfoManager.getInstance().tanks[me.campTankId];
			hpBottomText.text = myCampTank.healthPoint.toString();
			healthPointBar.percentage = 1;
			epBottomText.text = myCampTank.energyPoint.toString();
			energyPointBar.percentage = 1;
			//刷新武器
			var weaponSlot:WeaponSlot;
			for (var positionStr:String in weaponSlots)
			{
				weaponSlot = weaponSlots[positionStr];
				if (weaponSlot.parent != null)
					weaponSlot.parent.removeChild(weaponSlot);
			}
			for (var i:int = 0; i < 4; i++)
			{
				var comIndex:String = myCampTank.getEquipByPosition(6 + i);
				var position:int = 6 + i;
				if (comIndex != null)
				{
					var objIndexArr:Array = comIndex.split("_");
					weaponSlot = new WeaponSlot(objIndexArr[0], objIndexArr[1], objIndexArr[2]);
					weaponSlot.x = 28.8;
					weaponSlot.y = 262.5 + 85 * i;
					addChild(weaponSlot);
					weaponSlots[position] = weaponSlot;
				}
			}
		}
		
		public function triggerWeapon(position:int):void
		{
			var weaponSlot:WeaponSlot = weaponSlots[position];
			if (weaponSlot != null)
				weaponSlot.trigger();
		}
	}
}